package cn.ondraw.ragflow.service.impl;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.response.ApiResponse;
import cn.ondraw.ragflow.service.ConversationService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class ConversationServiceImpl implements ConversationService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/api/v1/conversation";

    /**
     * 设置对话信息
     *
     * @param request 设置对话的请求对象
     * @return API 响应对象
     */
    @Override
    public ApiResponse setConversation(SetConversationRequest request) {
        String interfaceUrl = URL_PREFIX + "/set";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        return (ApiResponse) response.getBody();
    }

    /**
     * 获取对话信息
     *
     * @param conversationId 对话 ID
     * @return API 响应对象
     */
    @Override
    public ApiResponse getConversation(String conversationId) {
        String url = URL_PREFIX + "/get?conversation_id=" + conversationId;
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return (ApiResponse) response.getBody();
    }

    /**
     * 获取 SSE 对话信息
     *
     * @param dialogId 对话 ID
     * @param token    访问令牌
     * @return API 响应对象
     */
    @Override
    public ApiResponse getSseConversation(String dialogId, String token) {
        String url = URL_PREFIX + "/getsse/" + dialogId;
        // 这里需要调整 PythonApiCaller 以支持设置请求头
        // 暂时简化处理
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return (ApiResponse) response.getBody();
    }

    /**
     * 删除对话信息
     *
     * @param conversationIds 要删除的对话 ID 列表
     * @return API 响应对象
     */
    @Override
    public ApiResponse rmConversation(List<String> conversationIds) {
        String interfaceUrl = URL_PREFIX + "/rm";
        Map<String, List<String>> body = new HashMap<>();
        body.put("conversation_ids", conversationIds);
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, body);
        return (ApiResponse) response.getBody();
    }

    /**
     * 列出对话信息
     *
     * @param dialogId 对话 ID
     * @return API 响应对象
     */
    @Override
    public ApiResponse listConversation(String dialogId) {
        String url = URL_PREFIX + "/list?dialog_id=" + dialogId;
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return (ApiResponse) response.getBody();
    }

    /**
     * 完成对话请求
     *
     * @param request 完成对话的请求对象
     * @return HTTP 响应实体
     */
    @Override
    public ResponseEntity<Object> completion(CompletionRequest request) {
        String interfaceUrl = URL_PREFIX + "/completion";
        if (request.isStream()) {
            // 这里需要调整 PythonApiCaller 以支持流式响应
            // 暂时简化处理
            ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_EVENT_STREAM);
            return new ResponseEntity<>(response.getBody(), headers, HttpStatus.OK);
        } else {
            ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
            return ResponseEntity.ok(response.getBody());
        }
    }

    /**
     * 文本转语音请求
     *
     * @param request 文本转语音的请求对象
     * @return HTTP 响应实体
     */
    @Override
    public ResponseEntity<Object> tts(TtsRequest request) {
        String interfaceUrl = URL_PREFIX + "/tts";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(response.getBody(), headers, HttpStatus.OK);
    }

    /**
     * 删除消息
     *
     * @param request 删除消息的请求对象
     * @return API 响应对象
     */
    @Override
    public ApiResponse deleteMsg(DeleteMsgRequest request) {
        String interfaceUrl = URL_PREFIX + "/delete_msg";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        return (ApiResponse) response.getBody();
    }

    /**
     * 点赞操作
     *
     * @param request 点赞请求对象
     * @return API 响应对象
     */
    @Override
    public ApiResponse thumbup(ThumbupRequest request) {
        String interfaceUrl = URL_PREFIX + "/thumbup";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        return (ApiResponse) response.getBody();
    }

    /**
     * 提问请求
     *
     * @param request 提问请求对象
     * @return HTTP 响应实体
     */
    @Override
    public ResponseEntity<Flux<String>> ask(AskRequest request) {
        String interfaceUrl = URL_PREFIX + "/ask";
        // 这里需要调整 PythonApiCaller 以支持流式响应
        // 暂时简化处理
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_EVENT_STREAM);
        return new ResponseEntity<>((Flux<String>) response.getBody(), headers, HttpStatus.OK);
    }

    /**
     * 生成思维导图
     *
     * @param request 生成思维导图的请求对象
     * @return API 响应对象
     */
    @Override
    public ApiResponse mindmap(MindmapRequest request) {
        String interfaceUrl = URL_PREFIX + "/mindmap";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        return (ApiResponse) response.getBody();
    }

    /**
     * 获取相关问题
     *
     * @param request 获取相关问题的请求对象
     * @return API 响应对象
     */
    @Override
    public ApiResponse relatedQuestions(RelatedQuestionsRequest request) {
        String interfaceUrl = URL_PREFIX + "/related_questions";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(interfaceUrl, request);
        return (ApiResponse) response.getBody();
    }
}
